import importlib
import os
from pyclbr import Function
import sys
from typing import Tuple
from routers.database.db import SealData, Session, get_engine, EquipmentData,PropData,SuitData


try:
    os.chdir(os.path.dirname(sys.argv[0]))
except Exception:
    pass


class equipment:
    id: str
    # iEid: str
    lv: int
    name: str
    rarity: str
    icon: str
    typeSon1Id: str
    typeSon2Id: str
    typeSon3Id: str
    desc: str
    suit: str
    STR: float
    INT: float
    AtkP: float
    AtkM: float

    def __init__(self, info={}) -> None:
        self.__dict__.update(info)


class prop:
    id: str
    # iEid: str
    name: str
    lv: int
    raity: str
    icon: str
    typeSon1Id: str
    desc: str

    def __init__(self, info={}) -> None:
        self.__dict__.update(info)

class seal:
    id: str
    name : str
    lv: int
    raity: str
    typeSon1Id: str
    typeSon2Id: str
    typeSon3Id: str
    desc: str

    def __init__(self, info={}) -> None:
        self.__dict__.update(info)

class suit:
    id: str
    suitID: str
    suitName: str
    needNum: int
    desc: str
    compatibility: str

    def __init__(self, info={}) -> None:
        self.__dict__.update(info)

def get_eq_info_data(version) -> list[equipment]:
    equs = []
    engine = get_engine(version)
    with Session(bind=engine) as session:
        for equ in session.query(EquipmentData).all():
            equs.append(equipment(equ.toDict()))
        pass
    engine.dispose()
    return equs

def get_prop_info_data(version) -> list[equipment]:
    props = []
    engine = get_engine(version)
    with Session(bind=engine) as session:
        for p in session.query(PropData).all():
            props.append(prop(p.toDict()))
        pass
    engine.dispose()
    return props


def get_suit_info_data(version) -> list[suit]:
    suits = []
    engine = get_engine(version)
    with Session(bind=engine) as session:
        for s in session.query(SuitData).all():
            suits.append(suit(s.toDict()))
        pass
    engine.dispose()
    return suits

def get_seal_info_data(version) -> list[seal]:
    seals = []
    engine = get_engine(version)
    with Session(bind=engine) as session:
        for s in session.query(SealData).all():
            seals.append(seal(s.toDict()))
        pass
    engine.dispose()
    return seals

class equipments:
    def __init__(self, version: int = 0) -> None:
        self.version = version
        self.info = get_eq_info_data(self.version)
        self.prop = get_prop_info_data(self.version)
        self.suit = get_suit_info_data(self.version)
        self.seal = get_seal_info_data(self.version)
        self.load_equ()
        pass

    def load_equ(self):
        self.equ_list = {}
        for i in self.info:
            self.equ_list[i.id] = i

    def get_equ_by_id(self, id: str) -> equipment:
        return self.equ_list.get(id)

    def get_equ_by_name(self, name: str) -> equipment:
        res = list(filter(lambda x: x.name == name, self.info))
        if len(res) > 0:
            return res[0]
        else:
            return None

    def get_func_by_id(self, id) -> Tuple[str, Function, int]:
        try:
            effects = importlib.import_module(
                "core.equipment.version_{}".format(self.version)
            )
        except Exception:
            effects = importlib.import_module("core.equipment.version_0")
        funcList = effects.funcList
        func_list = funcList.get(str(id), funcList["func_0"])
        priority = effects.priority.get(str(id), 0)
        return (id,func_list,priority)



global equ0
equ0 = equipments("0")
# equ1 = equipment_list("1")

# global equ1
# equ1 = equipment_list("1")


def get_equ(version="0"):
    if version is None or version == "":
        version = "0"
    try:
        temp = eval("equ{}".format(version))
    except Exception:
        temp = equ0
    return temp


def reload_equ():
    global equ0
    equ0 = equipments("0")
